<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var commonAddrbookDatagrid;
	$(function() {
		commonAddrbookDatagrid = $('#addrbook_commonAddrbookList_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/commonAddrbookAction!queryAddrbook.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20 ],
			sortName : 'name',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			striped : true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 80,
				checkbox : true
			}, {
				field : 'name',
				title : '姓名',
				width : 80,
				align : 'left',
				sortable : true
			}, {
				field : 'gender',
				title : '性别',
				width : 60,
				hidden : true
			}, {
				field : 'genderName',
				title : '性别',
				width : 60,
				align : 'center'
			}, {
				field : 'position',
				title : '职位',
				width : 100,
				align : 'left'
			}, {
				field : 'corporationName',
				title : '工作单位',
				width : 150,
				align : 'left'
			}, {
				field : 'mobile',
				title : '手机',
				width : 80,
				align : 'left'
			}, {
				field : 'qq',
				title : 'QQ',
				width : 80,
				align : 'left'
			}, {
				field : 'email',
				title : '电子邮件',
				width : 100,
				align : 'left'
			}, {
				field : 'phone',
				title : '固话',
				width : 80,
				align : 'left'
			}, {
				field : 'fax',
				title : '传真',
				width : 80,
				align : 'left'
			} ] ],
			toolbar : [ {
				text : '查询',
				iconCls : 'icon-search',
				handler : function() {
					queryAddrbook();
				}
			}, '-', {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					addAddrbook();
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					editAddrbook();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					removeAddrbook();
				}
			} ]
		});
		
		$('.datagrid-header div').css({
			'textAlign' : 'center'
		});

		function queryAddrbook() {
			$('#addrbook_commonAddrbookList_queryForm input').val('');
			$('#addrbook_commonAddrbookList_queryDialog').dialog('open');
		}

		function addAddrbook() {
			$('#addrbook_commonAddrbookAdd_addAddrbookForm input').val('');
			var dlg = $('<div style="padding:10px 20px;" />').dialog({
				width : 540,
				height : 280,
				href : '${pageContext.request.contextPath}/pages/addrbook/commonAddrbookAdd.jsp',
				modal : true,
				title : '联系人新增',
				buttons : [ {
					text : '保存',
					handler : function() {
						$('#addrbook_commonAddrbookAdd_addAddrbookForm').form('submit', {
							url : '${pageContext.request.contextPath}/commonAddrbookAction!addAddrbook.action',
							success : function(r) {
								var obj = jQuery.parseJSON(r);
								if (obj.success) {
									commonAddrbookDatagrid.datagrid('insertRow', {
										index : 0,
										row : obj.obj
									});
									dlg.dialog('close');
									$.messager.show({
										title : '提示',
										msg : obj.msg
									});
								} else {
									$.messager.alert('提示', obj.msg);
								}
							}
						});
					}
				}, {
					text : '取消',
					handler : function() {
						dlg.dialog('close');
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				},
				onLoad : function() {
					$('#addrbook_commonAddrbookAdd_addGroupCombobox').combobox('reload', '${pageContext.request.contextPath}/commonAddrbookAction!queryGroup.action');
				}
			});
		}

		function editAddrbook() {
			var rows = commonAddrbookDatagrid.datagrid('getChecked');
			if (rows.length == 1) {
				var dlg = $('<div style="padding:10px 20px;" />').dialog({
					width : 540,
					height : 280,
					href : '${pageContext.request.contextPath}/pages/addrbook/commonAddrbookEdit.jsp',
					modal : true,
					title : '联系人编辑',
					buttons : [ {
						text : '保存',
						handler : function() {
							$('#addrbook_commonAddrbookEdit_editAddrbookForm').form('submit', {
								url : '${pageContext.request.contextPath}/commonAddrbookAction!editAddrbook.action',
								success : function(r) {
									var obj = jQuery.parseJSON(r);
									if (obj.success) {
										commonAddrbookDatagrid.datagrid('updateRow', {
											index : commonAddrbookDatagrid.datagrid('getRowIndex', rows[0].id),
											row : obj.obj
										});
										dlg.dialog('close');
										$.messager.show({
											title : '提示',
											msg : obj.msg
										});
									} else {
										$.messager.alert('提示', obj.msg);
									}
								}
							});
						}
					}, {
						text : '取消',
						handler : function() {
							dlg.dialog('close');
						}
					} ],
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						$('#addrbook_commonAddrbookEdit_editAddrbookForm').form('load', rows[0]);
						$('#addrbook_commonAddrbookEdit_editAddrbookForm input[name=id]').val(rows[0].id);
						$('#addrbook_commonAddrbookEdit_editGroupCombobox').combobox('reload', '${pageContext.request.contextPath}/commonAddrbookAction!queryGroup.action');
						$('#addrbook_commonAddrbookEdit_editGroupCombobox').combobox('select', rows[0].groupId);
					}
				});
			} else {
				$.messager.alert('提示', '一次只能修改一条数据！');
			}
		}

		function removeAddrbook() {
			var rows = commonAddrbookDatagrid.datagrid('getChecked');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('确认', '确定要删除选择的数据吗？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/commonAddrbookAction!removeAddrbook.action',
							data : {
								ids : ids.join(',')
							},
							dataType : 'json',
							success : function(r) {
								commonAddrbookDatagrid.datagrid('load');
								commonAddrbookDatagrid.datagrid('unselectAll');
								$.messager.show({
									title : '提示',
									msg : r.msg,
								});
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！');
			}
		}
	});
</script>
<div id="addrbook_commonAddrbookList_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false">
		<table id="addrbook_commonAddrbookList_datagrid"></table>
	</div>
</div>
<div id="addrbook_commonAddrbookList_queryDialog" class="easyui-dialog" data-options="title:'联系人查询',closed:true,modal:true,
			buttons:[{
				text : '查询',
				handler : function(){
					$('#addrbook_commonAddrbookList_datagrid').datagrid('load', serializeObject($('#addrbook_commonAddrbookList_queryForm')));
				    $('#addrbook_commonAddrbookList_queryDialog').dialog('close');
				}
			},{
				text : '清空',
				handler : function(){
					$('#addrbook_commonAddrbookList_queryForm input').val('');
				}
			}]" style="width:380px;height:230px;padding:20px 10px;" align="center">
	<form id="addrbook_commonAddrbookList_queryForm" method="post">
		<table>
			<tr>
				<td style="text-align: right;">姓名：</td>
				<td><input name="name" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">手机：</td>
				<td><input name="mobile" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">QQ：</td>
				<td><input name="qq" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">电子邮件：</td>
				<td><input name="email" style="width:240px;" />
				</td>
			</tr>
		</table>
	</form>
</div>
<div id="addrbook_commonAddrbookList_groupDialog" class="easyui-dialog" data-options="title:'通讯录群组',closed:true,modal:true" style="width:400px;height:300px;">
	<div id="addrbook_commonAddrbookList_groupLayout" class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'center',border:false">
			<table id="addrbook_commonAddrbookList_groupDatagrid"></table>
		</div>
	</div>
</div>